package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "新增菜品接口")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/status/{status}")
    @ApiOperation("开启停售菜品")
    public Result setStatusDish(@PathVariable Integer status, Long id){
        log.info("[开启停售菜品] status:{},id:{}",status,id);
        dishService.getStatusById(status,id);
        return Result.success();
    }


    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> dishByCategoryId(Long categoryId){
        log.info("[根据菜品id查询]，categoryId", categoryId);

        List<Dish> list = dishService.dishByCategoryId(categoryId);
        return Result.success(list);
    }

    /**
     * 更新菜品
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("更新菜品")
    public Result updateDish(@RequestBody DishDTO dishDTO){
        log.info("[更新菜品] dishDto:{}",dishDTO);
        dishService.updateDishById(dishDTO);
        
        // 清楚缓存
        cleanRedisByCategroyId("dish:" + dishDTO.getCategoryId());
        return Result.success();
    }

    /**
     * 更新菜品根据id查询回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("更新菜品根据id查询")
    public Result<DishVO> getDishById(@PathVariable Long id){
        log.info("[根据id查询菜品] id:{}", id);
        DishVO dishVO = dishService.getDishById(id);
        return Result.success(dishVO);
    }

    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result deleteDishById(@RequestParam List<Long> ids){
        log.info("[批量删除菜品] Ids:{}", ids);
        dishService.deleteDishById(ids);

        // 全部删除
        cleanRedisByCategroyId("dish:*");
        return Result.success();
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> findDishPage(DishPageQueryDTO dishPageQueryDTO){
        log.info("[菜品分页查询] dishPageQueryDTO：{}", dishPageQueryDTO);
        PageResult pageResult = dishService.findDishPage(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    @PostMapping
    @ApiOperation("新增菜品")
    public Result addDish(@RequestBody DishDTO dishDTO){
        log.info("[新增菜品]{}", dishDTO);
        dishService.addDish(dishDTO);

        // 新增菜品需要清除内存
        cleanRedisByCategroyId("dish:" + dishDTO.getCategoryId());
        return Result.success();
    }

    private void cleanRedisByCategroyId(String keyPattern) {
        Set<String> keys = redisTemplate.keys(keyPattern);
        if (keys != null) {
            redisTemplate.delete(keys);
        }
    }


}
